package com.hangxin.system.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.hangxin.system.util.GlobalUtil;

public class FileTxtUploadServlet extends HttpServlet {	
	private static final long serialVersionUID = -7825355637448948879L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// 设置内存缓冲区，超过后写入临时文件
		factory.setSizeThreshold(10240000);//10M
		// 设置临时文件存储位置
		String strname = "";
		String path = GlobalUtil.getProperty("path");
		String filepath = request.getParameter("fjpath");
		String base = path + filepath;
		File file = new File(base);
		if(!file.exists())
			file.mkdirs();
		factory.setRepository(file);
		ServletFileUpload upload = new ServletFileUpload(factory);
		// 设置单个文件的最大上传值
		upload.setFileSizeMax(10002400000l);//100M
		// 设置整个request的最大值
		upload.setSizeMax(10002400000l);//1000M
		upload.setHeaderEncoding("gbk");
		
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		List<?> items  = null;
		try {
			items = upload.parseRequest(request);
			FileItem item = null;
			String fileName = null;
			for (int i = 0 ;i < items.size(); i++){
				item = (FileItem) items.get(i);
				fileName = base + File.separator + item.getName();
				// 保存文件
				if (!item.isFormField() && item.getName().length() > 0) {
					item.write(new File(fileName));
					File f = new File(fileName);      
			        if(f.isFile()&&f.exists())  
			        {       
			            InputStreamReader read = new InputStreamReader(new FileInputStream(f),"gbk");       
			            BufferedReader reader=new BufferedReader(read);       
			            String line;       
			            while ((line = reader.readLine()) != null)   
			            {        
			            	strname += line+"_";       
			            }         
			            read.close();      
			        }    
			}
			}
			strname = strname.substring(strname.indexOf("身份证校验_")+6,strname.length()-1);
			out.print(strname);
		} catch (Exception e) {
			e.printStackTrace();
			out.close();
		}finally{
			out.close();
		}
	}
	
}
